package com.codedancer.campus.client.api.confessionalWall;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.codedancer.campus.client.api.confessionalWall.ro.ConfessionWallAddRo;
import com.codedancer.campus.client.api.confessionalWall.ro.ConfessionalWallPageSearchRo;
import com.codedancer.campus.client.service.IConfessionalWallService;
import com.codedancer.campus.client.service.bo.confessionalWall.ConfessionWallAddBo;
import com.codedancer.campus.client.service.bo.confessionalWall.ConfessionalWallPageSearchBo;
import com.codedancer.campus.client.service.vo.confessionalWall.ConfessionalWallPageVo;
import com.codedancer.campus.client.service.vo.confessionalWall.ConfessionalWallVo;
import com.codedancer.campus.common.anno.KudosNumber;
import com.codedancer.campus.common.anno.ViewNumber;
import com.codedancer.campus.common.core.BaseApi;
import com.codedancer.campus.common.enums.KudosNumberKeyEnum;
import com.codedancer.campus.common.enums.ViewNumberKeyEnum;
import com.codedancer.campus.common.response.ApiResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/confessional-wall")
@CrossOrigin(methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE,
    RequestMethod.PUT}, maxAge = 3600)
@Api(value = "表白墙Api", description = "表白墙Api", tags = "表白墙Api")
public class ConfessionalWallApi extends BaseApi {

    @Autowired
    private IConfessionalWallService confessionalWallService;

    @PostMapping("/publish")
    @ApiOperation("发布表白墙")
    public ApiResponse<Void> publish(
        @RequestBody @Valid @ApiParam(value = "表白墙添加Ro", required = true)
        ConfessionWallAddRo confessionWallAddRo) {
        ConfessionWallAddBo confessionWallAddBo = BeanUtil
            .copyProperties(confessionWallAddRo, ConfessionWallAddBo.class);
        confessionWallAddBo.setCurrentUserUuid(currentLoginUuid());
        confessionalWallService.publish(confessionWallAddBo);
        return success();
    }

    @GetMapping("/page")
    @ApiOperation("分页查询表白墙")
    public ApiResponse<IPage<ConfessionalWallPageVo>> page(
        @ApiParam(value = "表白墙分页查询Ro", required = false)
        ConfessionalWallPageSearchRo confessionalWallPageSearchRo) {
        ConfessionalWallPageSearchBo confessionalWallPageSearchBo = BeanUtil
            .copyProperties(confessionalWallPageSearchRo, ConfessionalWallPageSearchBo.class);
        confessionalWallPageSearchBo.setCurrentUserUuid(currentLoginUuid());
        return success(confessionalWallService.page(confessionalWallPageSearchBo));
    }

    @GetMapping("/detail/{uuid}")
    @ApiOperation("表白墙详情")
    @ViewNumber(description = "获取表白墙详情", key = ViewNumberKeyEnum.CONFESSIONAL_WALL)
    public ApiResponse<ConfessionalWallVo> detail(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "表白墙uuid", required = true) String uuid) {
        return success(confessionalWallService.detail(uuid));
    }

    @GetMapping("/is-kudos/{uuid}")
    @ApiOperation("表白墙是否点赞")
    public ApiResponse<Boolean> isKudos(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "表白墙uuid", required = true) String uuid) {
        return success(confessionalWallService.isKudos(uuid, currentLoginUuid()));
    }

    @PostMapping("/kudos/{uuid}")
    @ApiOperation("点赞")
    @KudosNumber(description = "表白墙点赞", key = KudosNumberKeyEnum.CONFESSIONAL_WALL_KUDOS, isKudos = true)
    public ApiResponse<Void> kudos(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "表白墙uuid", required = true) String uuid) {
        confessionalWallService.kudos(uuid, currentLoginUuid());
        return success();
    }

    @PostMapping("/cancel-kudos/{uuid}")
    @ApiOperation("取消点赞")
    @KudosNumber(description = "表白墙取消点赞", key = KudosNumberKeyEnum.CONFESSIONAL_WALL_KUDOS, isKudos = false)
    public ApiResponse<Void> cancelKudos(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "表白墙uuid", required = true) String uuid) {
        confessionalWallService.cancelKudos(uuid, currentLoginUuid());
        return success();
    }

    @GetMapping("/page/user")
    @ApiOperation("分页用户查询表白墙")
    public ApiResponse<IPage<ConfessionalWallPageVo>> page2User(
        @ApiParam(value = "表白墙分页查询Ro", required = false)
        ConfessionalWallPageSearchRo confessionalWallPageSearchRo) {
        ConfessionalWallPageSearchBo confessionalWallPageSearchBo = BeanUtil
            .copyProperties(confessionalWallPageSearchRo, ConfessionalWallPageSearchBo.class);
        confessionalWallPageSearchBo.setCurrentUserUuid(currentLoginUuid());
        return success(confessionalWallService.page2User(confessionalWallPageSearchBo));
    }

    @PutMapping("/open-comment/{uuid}")
    @ApiOperation("开启评论")
    public ApiResponse<Void> openComment(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "表白墙uuid", required = true) String uuid) {
        confessionalWallService.openComment(uuid);
        return success();
    }

    @PutMapping("/close-comment/{uuid}")
    @ApiOperation("关闭评论")
    public ApiResponse<Void> closeComment(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "表白墙uuid", required = true) String uuid) {
        confessionalWallService.closeComment(uuid);
        return success();
    }

    @DeleteMapping("/delete/{uuid}")
    @ApiOperation("删除表白墙")
    public ApiResponse<Void> delete(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "表白墙uuid", required = true) String uuid) {
        confessionalWallService.delete(uuid);
        return success();
    }

    @PutMapping("/sync-liberty-square/{uuid}")
    @ApiOperation("同步自由广场")
    public ApiResponse<Void> syncLibertySquare(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "表白墙uuid", required = true) String uuid) {
        confessionalWallService.syncLibertySquare(uuid);
        return success();
    }

    @PutMapping("/cancel-sync-liberty-square/{uuid}")
    @ApiOperation("取消同步自由广场")
    public ApiResponse<Void> cancelSyncLibertySquare(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "表白墙uuid", required = true) String uuid) {
        confessionalWallService.cancelSyncLibertySquare(uuid);
        return success();
    }
}
